package cn.auto.utility;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.kit.HttpKit;
import com.jfinal.kit.PathKit;
import com.jfinal.upload.UploadFile;

import cn.auto.intercepter.CORSInterceptor;
import net.sf.json.JSONObject;

@Before(CORSInterceptor.class)
public class HelperController extends Controller {


	public JSONObject readRequstJson() {
		String jsonString = HttpKit.readData(getRequest());
		JSONObject jo = JSONObject.fromObject(jsonString);
		return jo;
	}

	public HashMap<String, String> getReqParameters(String[] paraArray) {
		HashMap<String, String> reqPara = new HashMap<>();
		for (int i = 0; i < paraArray.length; i++) {
			reqPara.put(paraArray[i], getPara(paraArray[i]));
		}
		return reqPara;
	}

	public void setUserSession(String user) {
		setSessionAttr("userName", user);
		setSessionAttr("loginTime", new Date().getTime());
	}

	public void responseResult(String sCode, String[] msg, Object bodys) {
		// String callback=getRequest().getParameter("callback");
		// String jsonp=null;
		if (sCode.equals(Status.SUCCESS)) {
			// jsonp=callback+"("+ResponseInfo.create(sCode, msg[0], bodys)+")";
			renderJson(ResponseInfo.create(sCode, msg[0], bodys));
		} else {
			// jsonp=callback+"("+ResponseInfo.create(sCode, msg[1])+")";
			renderJson(ResponseInfo.create(sCode, msg[1]));
		}
	}
	
	public void responseResult(String sCode, HashMap<String, String> msg, Object bodys) {
		if (sCode.equals(Status.SUCCESS)) {
			renderJson(ResponseInfo.create(sCode, msg.get(Status.SUCCESS), bodys));
		} else {
			renderJson(ResponseInfo.create(sCode, msg.get(sCode)));
		}
	}
	
	public void responseResult(String sCode, HashMap<String, String> msg) {
		if (sCode.equals(Status.SUCCESS)) {
			renderJson(ResponseInfo.create(sCode, msg.get(Status.SUCCESS)));
		} else {
			renderJson(ResponseInfo.create(sCode, msg.get(sCode)));
		}
	}

	public void responseResult(String sCode, String[] msg) {
		if (sCode.equals(Status.SUCCESS)) {
			renderJson(ResponseInfo.create(sCode, msg[0]));
		}
		if(sCode.equals(Status.FAILED)) {
			renderJson(ResponseInfo.create(sCode, msg[1]));
		}
		if(sCode.equals(Status.VERIFYERROR)){
			renderJson(ResponseInfo.create(sCode, msg[2]));
		}
	}
	
	public void responseResult(String sCode,HashMap<String, String> msg,List<?> body) {
		renderJson(ResponseInfo.createEui(sCode,msg.get(sCode),body));
	}
	
//	public void responseResult(String sCode, HashMap<String, String> msg) {
//		if (sCode.equals(Status.SUCCESS)) {
//			renderJson(ResponseInfo.create(sCode, msg.get(Status.SUCCESS)));
//		}
//		if(sCode.equals(Status.FAILED)) {
//			renderJson(ResponseInfo.create(sCode, msg.get(Status.FAILED)));
//		}
//		if(sCode.equals(Status.VERIFYERROR)){
//			renderJson(ResponseInfo.create(sCode, msg.get(Status.VERIFYERROR)));
//		}
//	}

	public String upLoad() {
		String path = PathKit.getWebRootPath().replace("\\", "/");
		path = path + "/upload/";// upload 为自定义的保存路径
		UploadFile file = getFile("upload", path, 5 * 1024 * 1024, "UTF-8");
		String fileurl = null;
		String upLoadUrl = null;
		Long fileName = System.currentTimeMillis();
		if (file != null) {
			String sufName = file.getFileName().substring(file.getFileName().lastIndexOf("."),
					file.getFileName().length());
			String imgPath = path + fileName + sufName;
			File dest = new File(imgPath);
			fileurl = "/upload/" + fileName + sufName;
			file.getFile().renameTo(dest);
			HashMap<String, String> config = configXmlLoad();

			upLoadUrl = config.get("serviceIP") + fileurl;
		}
		return upLoadUrl;

	}

	public HashMap<String, String> configXmlLoad() {
		Document doc = null;
		XPath xpath;

		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		dbf.setValidating(false);
		DocumentBuilder db = null;
		try {
			db = dbf.newDocumentBuilder();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			// 通过XMLload.class.getResource获取db.xml的绝对路径
			File f = new File(HelperController.class.getResource("/config.xml").getFile());
			doc = db.parse(f);
		} catch (SAXException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// 创建XPath对象
		XPathFactory factory = XPathFactory.newInstance();
		xpath = factory.newXPath();
		String serviceIP = null;
		String uploadDir = null;
		try {
			serviceIP = (String) xpath.evaluate("//serviceIP", doc, XPathConstants.STRING);
			uploadDir = (String) xpath.evaluate("//uploadDir", doc, XPathConstants.STRING);
		} catch (XPathExpressionException e) {
			e.printStackTrace();
		}

		HashMap<String, String> map = new HashMap<String, String>();
		map.put("serviceIP", serviceIP);
		map.put("uploadDir", uploadDir);
		return map;
	}
}
